It just like question ask you longest substring without repating character, it is a sliding winodw problem. you keep track of the winodw and curr max and update the res.
class Solution {
public int maxVowels(String s, int k) {
// sliding window
int left = 0, right = 0;
int res = 0;
int curr = 0;
while(right < s.length()){
char c = s.charAt(right);
if(c == 'a' || c == 'e' || c == 'i' || c =='o' || c == 'u'){
// vowel
curr ++;
}
// shrink window
while((right - left + 1) > k){
// it is too big
char shrink = s.charAt(left);
if(shrink == 'a' || shrink == 'e' || shrink == 'i' || shrink =='o' || shrink == 'u'){
// vowel
curr --;
}
left ++;
}
if((right - left + 1) <= k){
res = Math.max(res,curr);
}
right ++;
}
return res;
}
}
